<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<!--标记面板-->
<div class="modal fade" id="modal-mark-panel">
    <div class="modal-dialog" style="width: 550px;">
        <div class="modal-content">
            <div class="modal-header" style="padding-bottom: 0px;">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">编辑项目划库存标记信息</h4>
            </div>
            <div class="modal-body" style="padding-bottom: 5px;padding-top: 0px;">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="box">
                            <div class="box-header" style="padding-bottom: 0px;">

                            </div>
                            <div class="box-body">
                                <form id="modalForm" class="form-horizontal">
                                    <div class="row">
                                        <input type="hidden" name="pimId">
                                        <input type="hidden" name="pimBeforeItyId">
                                        <input type="hidden" name="pimAppStatus">
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">
                                                物料描述
                                            </label>
                                            <label>
                                                <input type="text" id="pimMatDesc" name="pimMatDesc"
                                                       class="form-control" readonly="readonly" style="width: 350px"/>
                                            </label>

                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">SAP物料编码</label>
                                            <label>
                                                <input type="text" id="pimSapCode" name="pimSapCode"
                                                       class="form-control" readonly="readonly" style="width: 350px"/>
                                            </label>
                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">项目编码</label>
                                            <label>
                                                <input type="text" id="pimBeforeProCode" name="pimBeforeProCode"
                                                       class="form-control"
                                                       readonly="readonly" style="width: 350px"/>
                                            </label>
                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">项目名称</label>
                                            <label>
                                                <input type="text" id="pimBeforeProName" name="pimBeforeProName"
                                                       class="form-control"
                                                       readonly="readonly" style="width: 350px"/>
                                            </label>
                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">可用数量</label>
                                            <label>
                                                <input type="text" id="pimBeforeAvaNum" name="pimBeforeAvaNum"
                                                       class="form-control"
                                                       readonly="readonly" style="width: 100px"/>
                                            </label>
                                            <label style="width: 90px;text-align: right; margin-left: 50px">可用重量</label>
                                            <label>
                                                <input type="text" id="pimBeforeAvaWeight" name="pimBeforeAvaWeight"
                                                       class="form-control"
                                                       readonly="readonly" style="width: 100px"/>
                                            </label>
                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">
                                                标记数量
                                            </label>
                                            <label>
                                                <input type="text" id="pimNum" name="pimNum"
                                                       class="form-control"
                                                       placeholder="请输入标记数量" oninput="checkValue(this)"
                                                       style="width: 100px">
                                            </label>
                                            <label style="width: 90px;text-align: right; margin-left: 50px">标记重量</label>
                                            <label>
                                                <input type="text" id="pimWeight" name="pimWeight"
                                                       class="form-control"
                                                       readonly="readonly" style="width: 100px"/>
                                            </label>
                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">
                                                划拨说明
                                            </label>
                                            <label>
                                                <input type="text" id="pimRemark" name="pimRemark"
                                                       class="form-control"
                                                       placeholder="请输入划拨说明"
                                                       style="width: 350px">
                                            </label>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer" style="padding-top: 5px;">
                <button type="button" class="btn btn-info" id="btn-mark-commit">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>

</div>
<section class="content-header">
    <h1>项目划库存申请</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" class="form-inline">
                        <div class="form-group">
                            <label>
                                项目编号
                            </label>
                            <input name="pimBeforeProCode" type="text" class="form-control text"
                                   placeholder="请输入项目编号"/>

                        </div>
                        <div class="form-group">
                            <label>
                                项目名称
                            </label>
                            <input name="pimBeforeProName" type="text" class="form-control text" maxlength="255"
                                   placeholder="请输入项目名称"/>

                        </div>
                        <div class="form-group">
                            <label>
                                物料描述
                            </label>
                            <input name="pimMatDesc" type="text" class="form-control text" maxlength="255"
                                   placeholder="请输入物料描述"/>
                        </div>

                        <div class="form-group">
                            <label>
                                标记人
                            </label>
                            <input name="pimProposerName" type="text" class="form-control text" maxlength="255"
                                   placeholder="请输入标记人信息"/>
                        </div>

                        <div class="form-group">
                            <label>
                                生产状态
                            </label>
                            <select name="pimBeforeProStatus" class="form-control select">
                                <option value="" selected="selected">全部</option>
                                <option value="-1">无状态</option>
                                <option value="1">未开始</option>
                                <option value="2">执行中</option>
                                <option value="3">已完成</option>
                                <option value="4">取消</option>
                                <option value="5">暂缓</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>
                                划拨状态
                            </label>
                            <select name="pimAppStatus" class="form-control select">
                                <option value="" >全部</option>
                                <option value="1" selected>未提交</option>
                                <option value="2">已驳回</option>
                                <option value="3">已提交</option>
                                <option value="4">已执行</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>
                                物料分类
                            </label>
                            <select name="ityMatType" id="ityMatType" class="form-control select">
                                <option value="">全部</option>
                                <option value="2">外协件</option>
                                <option value="4">铝料</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>标记日期</label>
                            <input type="text" class="form-control datepicker" id="startOdrMakeDate"
                                   name="startOdrMakeDate" readonly="readonly"/>
                            <span>至</span>
                            <input type="text" class="form-control datepicker" id="endOdrMakeDate"
                                   name="endOdrMakeDate" readonly="readonly"/>
                        </div>
                        <div class="form-group">
                            <button type="button" class="btn btn-success" id="btn-search">搜索
                            </button>
                        </div>
                    </form>
                </div>
                <div id="toolbar">
                    <button type="button" shiro:hasPermission="halinv:halprowar:edit"
                            class="btn btn-success" onclick="submitApply('')">
                        批量提交
                    </button>
                    <a href="/half/halInvManage/proToInvMark/createPage" shiro:hasPermission="halinv:halprowar:edit" class="btn btn-info" id="btn-add-flag">新增标记</a>
                </div>

                <div class="box-body">
                    <table id="bootstrap-table" class="table table-bordered " width="100%">
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var table;
    var applyStatusText = ["", "未提交", "已驳回", "已提交", "已执行"];
    var colors = ['', "bg-gray", "bg-red", "bg-yellow", "bg-green"];

    /**
     *@FunctionName: showEditPanel
     *@Description: 显示编辑模板
     *@Author: 徐一贺
     *@CreateDate: 2020/5/8 14:11
     *@Phone: 18241927380
     *@Param:
     *@Return:
     *@Version:
     */
    function showEditPanel(pimId) {
        $.post(ctx + 'half/halInvManage/proToInvMark/getHProjectInventoryMark', {
            pimId: pimId
        }, function (result) {
            if (result.type == web_status.SUCCESS) {
                $("#modalForm").fillData(result.data);
                js.table.refresh(table);
                js.modal.open("modal-mark-panel");
            } else if (result.type == web_status.FAIL) {
                js.modal.fail(result.msg);
            }
        }, 'json');
        js.modal.open();
    }
    /**
     *@FunctionName: checkValue
     *@Description: 修改标记数量  同时改变标记重量
     *@Author: TH
     */
    function checkValue(obj) {
        var weight = $("#modalForm [name='pimBeforeAvaWeight']").val();
        var avaNum = $("#modalForm [name='pimBeforeAvaNum']").val();
        var num = $("#modalForm [name='pimNum']").val();
        if(weight != null){
            var pimWeight = weight/avaNum*num;
            pimWeight = Math.round(pimWeight*1000)/1000;
            $("#modalForm [name='pimWeight']").val(pimWeight);
        }
    }

    /**
     *@FunctionName: submitAppy
     *@Description: 提交
     *@Author: 徐一贺
     *@CreateDate: 2020/4/21 12:35
     *@Phone: 18241927380
     *@Version: 1.0.0
     */
    function submitAppy(ipmId) {
        if (ipmId == '') {
            $('input[name="cb_change"]').each(function () {
                if (!$(this).prop("disabled")) {
                    if ($(this).prop("checked")) {
                        ipmId += $(this).attr("data") + ",";
                    }
                }
            })
            ipmId = ipmId.substring(0, ipmId.length - 1);
        }
        if (ipmId == '') {
            js.modal.warning("请选择要提交的库存划项目申请！");
            return;
        }
        js.modal.confirm("您确认要提交此标记吗？", function () {
            $.post(ctx + 'half/halInvManage/proToInvMark/submit', {
                ipmIds: ipmId
            }, function (result) {
                if (result.type == "SUCCESS") {
                    js.table.refresh(table);
                }
            }, 'json');
        });
    }


    /**
     *@FunctionName: search
     *@Description: 查询
     *@Author: 徐一贺
     *@CreateDate: 2020/4/10 16:10
     *@Phone: 18241927380
     *@Version: 1.0.0
     */
    function search() {
        js.table.search(table);
    }

    /**
     *@FunctionName: selectAll
     *@Description: 全选
     *@Author: 徐一贺
     *@CreateDate: 2020/4/8 15:20
     *@Phone: 18241927380
     *@Version: 1.0.0
     */
    function selectAll(obj) {
        $('input[name="cb_change"]').each(function () {
            if (!$(this).prop("disabled")) {
                if ($(obj).prop("checked")) {
                    $(this).prop("checked", true);
                }
                if ($(obj).prop("checked") == false) {
                    $(this).prop("checked", false);
                }
            }
        })

    }

    /**
     *@FunctionName: removeApply
     *@Description: 删除项目划库存申请
     *@Author: 徐一贺
     *@CreateDate: 2020/5/8 13:14
     *@Phone: 18241927380
     *@Param: [pimId] 主键
     *@Version: 1.0.0
     */
    function removeApply(pimId) {
        js.modal.confirm("尊敬的用户，您确认要删除此项目划库存申请吗？", function () {
            $.post(ctx + 'half/halInvManage/proToInvMark/remove', {
                pimIds: pimId
            }, function (result) {
                if (result.type == 'SUCCESS') {
                    js.table.refresh(table);
                    js.modal.success(result.msg);
                } else {
                    js.modal.fail(result.msg);
                }
            }, 'json');
        });
    }

    /**
     * 提交或批量提交
     * */
    function submitApply(pimId) {
        js.modal.confirm("尊敬的用户，您确认要提交选中的项目划库存申请吗？", function () {
            if (pimId == '') {
                $('input[name="cb_change"]').each(function () {
                    if (!$(this).prop("disabled")) {
                        if ($(this).prop("checked")) {
                            pimId += $(this).attr("data") + ",";
                        }
                    }
                })
                pimId = pimId.substring(0, pimId.length - 1);
            }
            if (pimId == '') {
                js.modal.warning("请选择要提交的项目划库存申请！");
                return;
            }
            $.post(ctx + 'half/halInvManage/proToInvMark/submit', {
                pimIds: pimId
            }, function (result) {
                if (result.type == 'SUCCESS') {
                    js.table.refresh(table);
                    js.modal.success(result.msg);
                } else {
                    js.modal.fail(result.msg);
                }
            }, 'json');
        });
    }

    $(function () {
        //加载列表数据
        table = js.table.init({
            id: 'bootstrap-table',
            url: ctx + "half/halInvManage/proToInvMark/applyList",
            showColumns: true,
            showExport: false,
            pageSize: 300,
            striped: false,
            uniqueId: "pimId",
            onLoadSuccess: function () {
                $('#bootstrap-table thead th').each(function () {
                    $(this).width(Math.floor($(this).width()));
                });
                $('#bootstrap-table tbody tr:first td').each(function () {
                    $(this).width(Math.floor($(this).width()));
                });
            },
            columns: [
                {
                    title: '   <input title="" id="selectAll" onclick="selectAll(this)" type="checkbox" />',
                    width: '80',
                    field: 'pimId',
                    formatter: function (value, row, index) {
                        var disabled = 'disabled="disabled"';
                        if (row.pimAppStatus == 1 || row.pimAppStatus == 2) {
                            disabled = "";
                        }
                        return '<input  style="width: 30px;height: 30px" ' + disabled + ' id="cb_change' + row.pimId + '" name="cb_change" data="' + row.pimId + '"  type="checkbox" />';
                    }
                },
                {
                    title: '序号', field: 'pimId', width: '40',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: '物料分类', field: 'ityMatType',
                    formatter: function (value, row, index) {
                        if (value == 2) {
                            return "外协件";
                        }else if(value == 3){
                            return "制作件";
                        }else if(value == 4){
                            return "铝料";
                        }else {
                            return value;
                        }
                    }},
                {title: '物料描述', field: 'pimMatDesc', width: '150'},
                {title: 'SAP物料编码', field: 'pimSapCode', width: '150'},
                {title: '图号', field: 'ityDrawingNumber'},
                {title: '标记数量', field: 'pimNum'},
                {title: '单位', field: 'pimNumUnit'},
                {title: '标记重量', field: 'pimWeight'},
                {title: '项目编号', field: 'pimBeforeProCode'},
                {title: '项目名称', field: 'pimBeforeProName'},
                {title: '生产状态', field: 'pimBeforeProStatus'},
                /*{
                    title: '仓库类型', field: 'pimItyType', width: '1', formatter: function (value, row, index) {
                        if (value == undefined) {
                            return "";
                        }
                        if (value == 'p') {
                            return "外购件库";
                        }
                        if (value == 'm') {
                            return "原料库";
                        }
                        if (value == 'h') {
                            return "成品库";
                        }
                        return value;
                    }
                },*/
                {title: '标记人', field: 'pimProposerName'},
                {title: '标记日期', field: 'pimDate',width:'90'},
                {
                    title: '划拨说明', field: 'pimRemark'
                    , formatter: function (value, row, index) {
                        if (value == undefined) {
                            return "";
                        }
                        return value;
                    }
                },
                {
                    title: '划拨状态', field: 'pimAppStatus'
                    , formatter: function (value, row, index) {
                        if (value == undefined) {
                            return "";
                        }
                        return '<span class="badge ' + colors[value] + '">' + applyStatusText[value] + '</span> ';

                    }
                },
                {
                    title: '操作', field: 'ipmId',width: '140',
                    formatter: function (value, row, index) {
                        var htmlText = "";
                        if ((row.pimAppStatus == 1 || row.pimAppStatus == 2) && hasP("halinv:halprowar:edit")) {
                            htmlText += '<button class="btn btn-success btn-xs" onclick="submitApply(' + row.pimId + ')">提交</button>';
                            htmlText += '<button class="btn btn-warning btn-xs" onclick="showEditPanel(' + row.pimId + ')">修改</button>';
                        }if((row.pimAppStatus == 1 || row.pimAppStatus == 2) && hasP("halinv:halprowar:del")){
                            htmlText += '<button onclick="removeApply(' + row.pimId + ')" class="btn btn-xs btn-danger">删除</button>';
                        }
                        return htmlText;
                    }
                }
            ]
        });
        /**
         * 表格搜索
         */
        $("#btn-search").on('click', function () {
            js.table.search(table);
        });

        // 验证初始化
        js.validate.init("modalForm", {
            fields: {
                pimNum: {
                    validators: {
                        notEmpty: {message: '请输入标记数量'},
                    }
                },
                pimRemark: {validators: {notEmpty: {message: '请输入划拨说明'}}},
            }
        });

        /**
         * 保存修改
         * */
        $("#btn-mark-commit").on('click', function () {
            if($("#pimNum").val() <= 0){
                js.modal.warning("标记数量必须大于0！")
                return;
            }
            js.submit({
                url: ctx + "half/halInvManage/proToInvMark/saveMark",
                data:
                    new FormData($("#modalForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-mark-panel");
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            });
        });
    });
</script>
</body>
</html>
